package com.study.hot.jetpack.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel

/**
 * <pre>
 *     author : fauks
 *     time   : 2020/03/29
 *     desc   :
 * </pre>
 */
class SaveStateViewModel(private val handler: SavedStateHandle) : ViewModel() {

    private val KEY = "num"

    fun getNum(): MutableLiveData<Int> {
        if (!handler.contains(KEY)) {
            handler.set(KEY, 0)
        }
        return handler.getLiveData(KEY)
    }

    fun addNum() {
        val liveData = handler.getLiveData<Int>(KEY)
        liveData.value = liveData.value?.plus(1)
    }
}